package tianyiyun;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String s = buf.readLine();
        String[] ss;
        int n = Integer.parseInt(s);
        PriorityQueue<int[]> heap = new PriorityQueue<int[]>((a, b) ->{
            if (a[0] != b[0]){
                return a[0] - b[0];
            }
            return a[1] - b[1];
        });
        for (int i = 0; i < n; i++){
            s = buf.readLine();
            ss = s.split(",");
            heap.offer(new int[]{Integer.parseInt(ss[0]), Integer.parseInt(ss[1])});
        }
        if (n == 0){
            System.out.println(0);
            return;
        }
        int[] tmp = heap.poll();
        int begin = tmp[0];
        int end = tmp[1];
        int res = 0;
        while (!heap.isEmpty()){
            tmp = heap.poll();
            if (tmp[0] < begin){

            } else {

            }
            if (tmp[0] < end){
                res += tmp[0] - begin < 0 ? 0 : tmp[0] - begin;
                if (tmp[1] < end){
                    begin = tmp[1];
                } else {
                    begin = end;
                    end = tmp[1];
                }
            } else {
                res += (end - begin);
                begin = tmp[0];
                end = tmp[1];
            }
        }
        res += (end - begin);
        System.out.println(res);
    }
}
